package sivin.camera

import android.graphics.SurfaceTexture
import android.hardware.camera2.CameraCharacteristics
import android.hardware.camera2.CameraManager
import android.util.Size
import androidx.annotation.StringDef

const val CAMERA_FACING_BACK = "Back"
const val CAMERA_FACING_FRONT = "Front"
const val CAMERA_FACING_EXTERNAL = "External"

@StringDef(CAMERA_FACING_BACK, CAMERA_FACING_FRONT, CAMERA_FACING_EXTERNAL)
@Retention(AnnotationRetention.SOURCE)
annotation class CameraFacing

class CameraInfo(
    val facing: String,
    val cameraId: String,
    val size: Size,
    val fps: Int
)


object CameraUtil {

    private fun lensFacingString(value: Int) = when (value) {
        CameraCharacteristics.LENS_FACING_BACK -> CAMERA_FACING_BACK
        CameraCharacteristics.LENS_FACING_FRONT -> CAMERA_FACING_FRONT
        CameraCharacteristics.LENS_FACING_EXTERNAL -> CAMERA_FACING_EXTERNAL
        else -> "Unknown"
    }

    private fun enumerateVideoCameras(cameraManager: CameraManager): List<CameraInfo> {
        val availableCameras: MutableList<CameraInfo> = mutableListOf()
        cameraManager.cameraIdList.forEach { id ->
            val characteristics = cameraManager.getCameraCharacteristics(id)
            val facing = lensFacingString(
                characteristics.get(CameraCharacteristics.LENS_FACING)!!
            )

            val capabilities = characteristics.get(
                CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES
            )!!

            //获取相机设备支持的输出配置信息：视频格式，尺寸，相关的帧率限制等
            val cameraConfig = characteristics.get(
                CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP
            )!!

            /*
                判断相机是否向后兼容：比如支持类似Camera1中的基本API，
                如果支持，则表示这个设备至少支持以下特性：
                    1.支持拍摄静态图像
                    2.支持常见的自动对焦模式
                    3.支持自动调节曝光。
                    4.支持自动调节白平衡
                    5.支持常见的图像和视频格式（如JPEG、YUV、RGB等）
             */

            if (capabilities.contains(
                    CameraCharacteristics
                        .REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE
                )
            ) {
                //查询这个摄像头支持的在SurfaceTexture下，即：预览，支持的输出能力
                val targetClass = SurfaceTexture::class.java
                cameraConfig.getOutputSizes(targetClass).forEach { size ->
                    //获取一大小为size下，输出一帧数据需要多少事件
                    val secondsPerFrame =
                        cameraConfig.getOutputMinFrameDuration(targetClass, size) /
                                1_000_000_000.0
                    // 计算每秒能输出多少帧
                    val fps = if (secondsPerFrame > 0) (1.0 / secondsPerFrame).toInt() else 0
                    val fpsLabel = if (fps > 0) "$fps" else "N/A"

                    availableCameras.add(
                        CameraInfo(
                            facing, id, size, fps
                        )
                    )
                }
            }
        }
        return availableCameras
    }

    fun chooseAvailableResolution(@CameraFacing desireFacing: String, desireSize: Size) {
        if (desireFacing == CAMERA_FACING_BACK) {

        }

    }
}